﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using UnityEditorInternal;
    using UnityEngine;

    internal class Toolbar : GUIView
    {
        public static Toolbar get = null;
        private const string kAll = "Everything";
        private const string kEditLayers = "Edit Layers...";
        private const string kNone = "Nothing";
        private static string s_LastLoadedLayoutName = null;
        private static GUIContent s_LayerContent = EditorGUIUtility.TextContent("ToolbarLayers");
        private static int s_LayerMaskHash = "LayerMaskField".GetHashCode();
        private int[] s_LayerMasks;
        private string[] s_LayerNames;
        private int s_MaskID = -1;
        private int s_MaskSelection = -1;
        private static GUIContent[] s_PivotIcons = new GUIContent[] { EditorGUIUtility.TextContent("ToolHandleCenter"), EditorGUIUtility.TextContent("ToolHandlePivot") };
        private static GUIContent[] s_PivotRotation = new GUIContent[] { EditorGUIUtility.TextContent("ToolHandleLocal"), EditorGUIUtility.TextContent("ToolHandleGlobal") };
        private static GUIContent[] s_PlayIcons = new GUIContent[] { EditorGUIUtility.IconContent("PlayButton"), EditorGUIUtility.IconContent("PauseButton"), EditorGUIUtility.IconContent("StepButton"), EditorGUIUtility.IconContent("PlayButtonProfile"), EditorGUIUtility.IconContent("PlayButton On"), EditorGUIUtility.IconContent("PauseButton On"), EditorGUIUtility.IconContent("StepButton On"), EditorGUIUtility.IconContent("PlayButtonProfile On"), EditorGUIUtility.IconContent("PlayButton Anim"), EditorGUIUtility.IconContent("PauseButton Anim"), EditorGUIUtility.IconContent("StepButton Anim"), EditorGUIUtility.IconContent("PlayButtonProfile Anim") };
        private int[] s_SelectedIndices;
        private static GUIContent[] s_ShownToolIcons = new GUIContent[4];
        private static GUIContent[] s_ToolIcons = new GUIContent[] { EditorGUIUtility.IconContent("MoveTool"), EditorGUIUtility.IconContent("RotateTool"), EditorGUIUtility.IconContent("ScaleTool"), EditorGUIUtility.IconContent("MoveTool On"), EditorGUIUtility.IconContent("RotateTool On"), EditorGUIUtility.IconContent("ScaleTool On") };
        private static GUIContent[] s_ViewToolIcons = new GUIContent[] { EditorGUIUtility.IconContent("ViewToolOrbit"), EditorGUIUtility.IconContent("ViewToolMove"), EditorGUIUtility.IconContent("ViewToolZoom"), EditorGUIUtility.IconContent("ViewToolOrbit"), EditorGUIUtility.IconContent("ViewToolOrbit On"), EditorGUIUtility.IconContent("ViewToolMove On"), EditorGUIUtility.IconContent("ViewToolZoom On"), EditorGUIUtility.IconContent("ViewToolOrbit On") };
        private bool t1;
        private bool t2;
        private bool t3;

        public float CalcHeight()
        {
            return 30f;
        }

        private void CalcValidLayers(int mask)
        {
            ArrayList list = new ArrayList();
            ArrayList list2 = new ArrayList();
            ArrayList list3 = new ArrayList();
            list.Add("Everything");
            list.Add("Nothing");
            bool flag = true;
            bool flag2 = true;
            for (int i = 0; i < 0x20; i++)
            {
                string layerName = InternalEditorUtility.GetLayerName(i);
                if (layerName != string.Empty)
                {
                    list.Add(layerName);
                    list2.Add(i);
                    if ((mask & (((int) 1) << i)) != 0)
                    {
                        list3.Add(list2.Count + 1);
                        flag2 = false;
                    }
                    else
                    {
                        flag = false;
                    }
                }
            }
            if (flag)
            {
                list3.Add(list.IndexOf("Everything"));
            }
            if (flag2)
            {
                list3.Add(list.IndexOf("Nothing"));
            }
            list.Add(string.Empty);
            list.Add("Edit Layers...");
            this.s_LayerNames = list.ToArray(typeof(string)) as string[];
            this.s_LayerMasks = list2.ToArray(typeof(int)) as int[];
            this.s_SelectedIndices = list3.ToArray(typeof(int)) as int[];
        }

        private void DoLayersDropDown()
        {
            GUI.changed = false;
            Tools.visibleLayers = this.LayerMaskField(Tools.visibleLayers, s_LayerContent, "DropDown");
            if (GUI.changed)
            {
                foreach (SceneView view in Resources.FindObjectsOfTypeAll(typeof(SceneView)))
                {
                    view.Repaint();
                }
            }
        }

        private void DoLayoutDropDown()
        {
            Rect position = GUILayoutUtility.GetRect(s_LayerContent, "DropDown");
            if (EditorGUI.ButtonMouseDown(position, new GUIContent(lastLoadedLayoutName), FocusType.Passive, "DropDown"))
            {
                Vector2 vector = GUIUtility.GUIToScreenPoint(new Vector2(position.x, position.y));
                position.x = vector.x;
                position.y = vector.y;
                EditorUtility.Internal_DisplayPopupMenu(position, "Window/Layouts", this, 0);
            }
            GUILayout.EndHorizontal();
        }

        private void DoPlayButtons(bool isOrWillEnterPlaymode)
        {
            bool isPlaying = EditorApplication.isPlaying;
            GUI.changed = false;
            int index = !isPlaying ? 0 : 4;
            if (AnimationUtility.InAnimationMode())
            {
                index = 8;
            }
            Color color = GUI.color + new Color(0.01f, 0.01f, 0.01f, 0.01f);
            GUI.contentColor = new Color(1f / color.r, 1f / color.g, 1f / color.g, 1f / color.a);
            GUILayout.Toggle(isOrWillEnterPlaymode, s_PlayIcons[index], "CommandLeft", new GUILayoutOption[0]);
            GUI.backgroundColor = Color.white;
            if (GUI.changed)
            {
                TogglePlaying();
                GUIUtility.ExitGUI();
            }
            GUI.changed = false;
            bool flag2 = GUILayout.Toggle(EditorApplication.isPaused, s_PlayIcons[index + 1], "CommandMid", new GUILayoutOption[0]);
            if (GUI.changed)
            {
                EditorApplication.isPaused = flag2;
                GUIUtility.ExitGUI();
            }
            if (GUILayout.Button(s_PlayIcons[index + 2], "CommandRight", new GUILayoutOption[0]))
            {
                EditorApplication.Step();
                GUIUtility.ExitGUI();
            }
        }

        private void DoToolButtons()
        {
            GUI.changed = false;
            int selected = !Tools.viewToolActive ? ((int) Tools.current) : 0;
            for (int i = 1; i < 4; i++)
            {
                s_ShownToolIcons[i] = s_ToolIcons[(i != selected) ? (i - 1) : (i + 2)];
                s_ShownToolIcons[i].tooltip = s_ToolIcons[i - 1].tooltip;
            }
            s_ShownToolIcons[0] = s_ViewToolIcons[((int) Tools.viewTool) + ((selected != 0) ? 0 : 4)];
            selected = GUI.Toolbar(new Rect(10f, 5f, 130f, 24f), selected, s_ShownToolIcons, "Command");
            if (GUI.changed)
            {
                Tools.current = (Tool) selected;
            }
            Tools.pivotMode = (PivotMode) EditorGUI.CycleButton(new Rect(160f, 8f, 64f, 18f), (int) Tools.pivotMode, s_PivotIcons, "ButtonLeft");
            if (Tools.current == Tool.Scale)
            {
                GUI.enabled = false;
            }
            PivotRotation rotation = (PivotRotation) EditorGUI.CycleButton(new Rect(224f, 8f, 64f, 18f), (int) Tools.pivotRotation, s_PivotRotation, "ButtonRight");
            if (Tools.pivotRotation != rotation)
            {
                Tools.pivotRotation = rotation;
                if (rotation == PivotRotation.Global)
                {
                    Tools.ResetGlobalHandleRotation();
                }
            }
            if (Tools.current == Tool.Scale)
            {
                GUI.enabled = true;
            }
            if (GUI.changed)
            {
                Tools.RepaintAllToolViews();
            }
        }

        private static void InternalWillTogglePlaymode()
        {
            InternalEditorUtility.RepaintAllViews();
        }

        private int LayerMaskField(int mask, GUIContent content, GUIStyle style)
        {
            int num = EditorGUI.GetControlID(s_LayerMaskHash, EditorGUIUtility.native, base.position);
            Rect position = GUILayoutUtility.GetRect(content, style);
            if (EditorGUI.ButtonMouseDown(position, content, FocusType.Passive, style))
            {
                this.s_MaskID = num;
                this.s_MaskSelection = -1;
                this.CalcValidLayers(mask);
                EditorUtility.DisplayCustomMenu(position, this.s_LayerNames, this.s_SelectedIndices, new EditorUtility.SelectMenuItemFunction(this.ToggleBitDelegate), null);
            }
            if (((Event.current.type != EventType.Repaint) && (this.s_MaskSelection != -1)) && (this.s_MaskID == num))
            {
                if (this.s_MaskSelection == 0)
                {
                    mask = 0x7fffffff;
                }
                else if (this.s_MaskSelection == 1)
                {
                    mask = 0;
                }
                else
                {
                    this.s_MaskSelection -= 2;
                    if (this.s_MaskSelection >= this.s_LayerMasks.Length)
                    {
                        Selection.activeObject = EditorApplication.tagManager;
                    }
                    else
                    {
                        mask ^= ((int) 1) << this.s_LayerMasks[this.s_MaskSelection];
                    }
                }
                this.s_MaskSelection = this.s_MaskID = -1;
                GUI.changed = true;
            }
            return mask;
        }

        public void OnDisable()
        {
            EditorApplication.modifierKeysChanged = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.modifierKeysChanged, new EditorApplication.CallbackFunction(this.Repaint));
            EditorApplication.undoRedoPerformed = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.undoRedoPerformed, new EditorApplication.CallbackFunction(this.OnSelectionChange));
        }

        public void OnEnable()
        {
            EditorApplication.modifierKeysChanged = (EditorApplication.CallbackFunction) Delegate.Combine(EditorApplication.modifierKeysChanged, new EditorApplication.CallbackFunction(this.Repaint));
            EditorApplication.undoRedoPerformed = (EditorApplication.CallbackFunction) Delegate.Combine(EditorApplication.undoRedoPerformed, new EditorApplication.CallbackFunction(this.OnSelectionChange));
            get = this;
            s_LastLoadedLayoutName = EditorPrefs.GetString("LastLoadedLayoutName", "Layout");
        }

        protected override bool OnFocus()
        {
            return false;
        }

        private void OnGUI()
        {
            bool isPlayingOrWillChangePlaymode = EditorApplication.isPlayingOrWillChangePlaymode;
            if (isPlayingOrWillChangePlaymode)
            {
                GUI.color = (Color) HostView.kPlayModeDarken;
            }
            GUIStyle style = "AppToolbar";
            if (Event.current.type == EventType.Repaint)
            {
                style.Draw(new Rect(0f, 0f, base.position.width, base.position.height), false, false, false, false);
            }
            this.DoToolButtons();
            float num = 100f;
            float a = (base.position.width - num) / 2f;
            GUILayout.BeginArea(new Rect(Mathf.Max(a, 373f), 5f, 120f, 24f));
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            this.DoPlayButtons(isPlayingOrWillChangePlaymode);
            GUILayout.EndHorizontal();
            GUILayout.EndArea();
            float num3 = 220f;
            a = base.position.width - num3;
            a = Mathf.Max(a, 440f);
            GUILayout.BeginArea(new Rect(a, 7f, (base.position.width - a) - 10f, 24f));
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            this.DoLayersDropDown();
            GUILayout.Space(6f);
            this.DoLayoutDropDown();
            GUILayout.EndArea();
            EditorGUI.ShowRepaints();
        }

        private void OnSelectionChange()
        {
            Tools.OnSelectionChange();
        }

        internal static void RepaintToolbar()
        {
            if (get != null)
            {
                get.Repaint();
            }
        }

        private void ToggleBitDelegate(object userData, string[] options, int selected)
        {
            this.s_MaskSelection = selected;
        }

        private static void TogglePlaying()
        {
            bool flag = !EditorApplication.isPlaying;
            EditorApplication.isPlaying = flag;
            InternalWillTogglePlaymode();
        }

        internal static string lastLoadedLayoutName
        {
            get
            {
                return s_LastLoadedLayoutName;
            }
            set
            {
                if (value != null)
                {
                    s_LastLoadedLayoutName = value;
                    EditorPrefs.SetString("LastLoadedLayoutName", value);
                }
                else
                {
                    EditorPrefs.DeleteKey("LastLoadedLayoutName");
                    s_LastLoadedLayoutName = "Layout";
                }
                if (get != null)
                {
                    get.Repaint();
                }
            }
        }
    }
}

